<?php
namespace app\modules\v1\controllers;
use Yii;
use yii\rest\ActiveController;
use yii\web\Response;
use app\modules\v1\models\MerchantWeixin;
use app\modules\v1\models\Merchant;

class CashdeskController extends ActiveController
{
	
    public $modelClass = 'app\modules\v1\models\MerchantWeixin';
    
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['update'], $actions['create'], $actions['delete'], $actions['view']);
        return $actions;
    } 

    public function actionCreate()
    {
    	$res = array();
    	if(Yii::$app->request->isPost){
    		$openid 	= isset(Yii::$app->request->post()['openid'])?Yii::$app->request->post()['openid']:'';
    		if(empty($openid)){
    			$res['code'] = '10402';
    			$res['msg']  = '请取消关注后重新关注公众号';
    			$response			= Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
    		//用户微信数据
    		$weixininfo = MerchantWeixin::findByOpenid($openid);
    		if(empty($weixininfo)){
    			$res['code'] = '10402';
    			$res['msg']  = '请取消关注后重新关注公众号';
    			$response	 = Yii::$app->response;
    			$response->format	= Response::FORMAT_JSON;
    			$response->data		= $res;
    			return $response;
    		}
    		$headimg    = !empty($weixininfo->headimg)?$weixininfo->headimg:'';
    		$nickname	= !empty($weixininfo->nickname)?base64_decode($weixininfo->nickname):'';
    		$name		= !empty($weixininfo->nickname)?$weixininfo->nickname:'';
    		$status 	= '0';
    		$non_fee 	= '0.00';
    		$today_deal = '0.00';
    		$month_deal = '0.00';
    		$totla_deal = '0.00';
    		$is_pay		= '1';
    		//认证信息  0未认证；1认证正在审核；2认证未通过；3认证通过  		
    		$merchant1 = Merchant::findOne($weixininfo->merchant_id);
    		if(!empty($merchant1)){
    			if(!empty($merchant1->merchant_name)){
    				$nickname = $merchant1->merchant_name;
    			}
    			$status = $merchant1->check_status_jinjian;
    			if ($status == 3){
    				$name		= !empty($merchant1->merchant_name)?$merchant1->merchant_name:'';
    				$headimg	= !empty($merchant1->head_img)?$merchant1->head_img:'';
    				$non_fee 	= !empty( $merchant1->free_balance)? $merchant1->free_balance:'';
    				$today      = date('Y-m-d');
    				$month		= date('Y-m-01', strtotime(date("Y-m-d")));
    				$query_day  = "SELECT SUM(money) as a  FROM `merchant_bill_details` WHERE merchant_id='{$weixininfo->merchant_id}' AND pay_type !='4'  AND status ='0' AND date_time >= '{$today}'";
    				$query_mon  = "SELECT SUM(money) as a FROM `merchant_bill_details` WHERE merchant_id='{$weixininfo->merchant_id}' AND pay_type !='4' AND status ='0' AND date_time >= '{$month}'";
    				$query		= "SELECT SUM(money) as a FROM `merchant_bill_details` WHERE merchant_id='{$weixininfo->merchant_id}' AND pay_type !='4' AND status ='0' ";
    				$connection = Yii::$app->db;
    				$command1 	= $connection->createCommand($query_day)->queryOne();
    				$today_deal = (string)$command1['a'];  //今日交易
    				$command2 	= $connection->createCommand($query_mon)->queryOne();
    				$month_deal = (string)$command2['a'];  //本月交易
    				$command3 	= $connection->createCommand($query)->queryOne();
    				$totla_deal = (string)$command3['a'];  //累计交易
    			}	
    		}
    		$res['data'] = [
	    		'openid'  		=> (string)$openid,
	    		'headimg' 		=> (string)$headimg,
	    		'nickname' 		=> (string)$nickname,
	    		'merchant_name' => (string)$name,
	    		'non_fee' 		=> (string)$non_fee,
	    		'today_deal'	=> (string)$today_deal,
	    		'month_deal'	=> (string)$month_deal,
	    		'totla_deal'	=> (string)$totla_deal,
	    		'identy_status' => (string)$status,
	    		'is_pay' 		=> (string)$is_pay,
    		];
    		$res['data'] = $res;
    		$res['code'] = '10000';
    		$res['msg']  = '成功';
    		$response=Yii::$app->response;
    		$response->format=Response::FORMAT_JSON;
    		$response->data=$res;
    		
    	}else{
    		$res['code'] = '10402';
    		$res['msg']  = '请重新关注公众号!';
    		$response=Yii::$app->response;
    		$response->format=Response::FORMAT_JSON;
    		$response->data=$res;
    	}
    }

}